﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsSupportTicketsTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// SupportTickets Business Object
    /// </summary>

    public class SupportTickets : BOBase
    {


        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected SupportTickets() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the SupportTickets.
        /// </summary>
        public string TicketID
        {
            get { return _TicketID; }
            set
            {
                if (_TicketID != value)
                {
                    if (OnPropertyChanging("TicketID", _TicketID, value))
                    {
                        string oldValue = _TicketID;
                        _TicketID = value;

                        OnPropertyChanged("TicketID", oldValue, value);
                    }
                }
            }
        }  private string _TicketID = null;




        /// <summary>
        /// PKID of the User.
        /// </summary>
        public int Ref_UserID
        {
            get { return _ref_userid; }
            set
            {
                if (_ref_userid != value)
                {
                    if (OnPropertyChanging("Ref_UserID", _ref_userid, value))
                    {
                        int oldValue = _ref_userid;
                        _ref_userid = value;

                        OnPropertyChanged("Ref_UserID", oldValue, value);
                    }
                }
            }
        }  private int _ref_userid = -1;



        /// <summary>
        /// TicketSubject.
        /// </summary>
        public string TicketSubject
        {
            get { return _ticketsubject; }
            set
            {
                if (_ticketsubject != value)
                {
                    if (OnPropertyChanging("TicketSubject", _ticketsubject, value))
                    {
                        string oldValue = _ticketsubject;
                        _ticketsubject = value;
                        OnPropertyChanged("TicketSubject", oldValue, value);
                    }
                }
            }
        }  private string _ticketsubject = null;


        /// <summary>
        /// TicketQuestion.
        /// </summary>
        public string TicketQuestion
        {
            get { return _ticketquestion; }
            set
            {
                if (_ticketquestion != value)
                {
                    if (OnPropertyChanging("TicketQuestion", _ticketquestion, value))
                    {
                        string oldValue = _ticketquestion;
                        _ticketquestion = value;
                        OnPropertyChanged("TicketQuestion", oldValue, value);
                    }
                }
            }
        }  private string _ticketquestion = null;





        /// <summary>
        /// Severity.
        /// </summary>
        public string Severity
        {
            get { return _severity; }
            set
            {
                if (_severity != value)
                {
                    if (OnPropertyChanging("Severity", _severity, value))
                    {
                        string oldValue = _severity;
                        _severity = value;
                        OnPropertyChanged("Severity", oldValue, value);
                    }
                }
            }
        }  private string _severity = null;





        /// <summary>
        /// TicketAttachments.
        /// </summary>
        public string TicketAttachments
        {
            get { return _ticketattachments; }
            set
            {
                if (_ticketattachments != value)
                {
                    if (OnPropertyChanging("TicketAttachments", _ticketattachments, value))
                    {
                        string oldValue = _ticketattachments;
                        _ticketattachments = value;
                        OnPropertyChanged("TicketAttachments", oldValue, value);
                    }
                }
            }
        }  private string _ticketattachments = null;


        /// <summary>
        /// Date/time at which the SupportTickets was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addeddate; }
            set
            {
                if (_addeddate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addeddate, value))
                    {
                        DateTime? oldValue = _addeddate;
                        _addeddate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addeddate = null;


        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty SupportTickets object.
        /// </summary>
        public static SupportTickets CreateNew()
        {
            SupportTickets obj = new SupportTickets();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.AddedDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static SupportTickets CreateNew(SupportTicketsCollection collection)
        {
            SupportTickets obj = CreateNew();            
            collection.Add(obj);
            return (obj);
        }

        #endregion

        #region GetAllSupportTickets

        /// <summary>
        /// Returns a collection of all SupportTickets.
        /// </summary>
        public static SupportTicketsCollection GetAllSupportTickets()
        {
            SupportTicketsTableAdapter adapter = new SupportTicketsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsSupportTickets.SupportTicketsDataTable tbl = adapter.GetData(null);

            SupportTicketsCollection list = new SupportTicketsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsSupportTickets.SupportTicketsRow row in tbl.Rows)
                {
                    SupportTickets SupportTickets = new SupportTickets();
                    RelationalToObject(SupportTickets, row);
                    list.Add(SupportTickets);
                }
            }

            return (list);
        }

        #endregion

        #region GetSupportTicketsByID

        /// <summary>
        /// Returns an SupportTickets object for the specified 'TicketID' or null if no SupportTickets with the specified parameter(s) exists.
        /// </summary>
        public static SupportTickets GetSupportTicketsByID(string TicketID)
        {
            SupportTicketsTableAdapter adapter = new SupportTicketsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsSupportTickets.SupportTicketsDataTable tbl = adapter.GetData(TicketID);

            if (tbl.Rows.Count == 1)
            {
                SupportTickets SupportTickets = new SupportTickets();
                RelationalToObject(SupportTickets, tbl.Rows[0]);
                return (SupportTickets);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'TicketID' returned more than one record.");

            return (null);
        }

        #endregion        

        #region GetSupportTicketsByUserID

        /// <summary>
        /// Returns a collection of all SupportTickets.
        /// </summary>
        public static SupportTicketsCollection GetSupportTicketsByUserID(int userid)
        {
            SupportTicketsTableAdapter adapter = new SupportTicketsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsSupportTickets.SupportTicketsDataTable tbl = adapter.GetDataByUserID(userid);

            SupportTicketsCollection list = new SupportTicketsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsSupportTickets.SupportTicketsRow row in tbl.Rows)
                {
                    SupportTickets SupportTickets = new SupportTickets();
                    RelationalToObject(SupportTickets, row);
                    list.Add(SupportTickets);
                }
            }

            return (list);
        }

        #endregion        

        #region Update

        /// <summary>
        /// Updates the specified SupportTickets.
        /// </summary>
        public static void Update(SupportTickets obj)
        {
            bool updateSupportTickets = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateSupportTickets = (obj.ObjState != ObjectState.Unchanged);

                if (!updateSupportTickets)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsSupportTickets.SupportTicketsDataTable tbl = null;
            if (updateSupportTickets)
            {
                SupportTicketsTableAdapter adapter = new SupportTicketsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsSupportTickets.SupportTicketsDataTable();
                dsSupportTickets.SupportTicketsRow row = tbl.NewSupportTicketsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (TicketID == null))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("TicketID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((TicketSubject == null) || (TicketSubject.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("TicketSubject", "TicketSubject is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("SupportTickets: TicketID={0}, TicketSubject={1}", TicketID, TicketSubject));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SupportTickets objects.
    /// </summary>

    public class SupportTicketsCollection : List<SupportTickets>
    {
        #region Types

        public class UserComparer : IComparer<SupportTickets>
        {
            public int Compare(SupportTickets a1, SupportTickets a2)
            {
                return (String.Compare(a1.TicketID, a2.TicketID));
            }
        }

        #endregion

        #region Public Methods

        #region FindByTicketID

        public SupportTickets FindByTicketID(string TicketID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].TicketID == TicketID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
